package com.cloud.upms.service.impl;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloud.common.core.constant.CommonConstants;
import com.cloud.common.core.exception.ProjectException;
import com.cloud.api.upms.domain.menu.MenuAddUpdateReq;
import com.cloud.api.upms.enums.MenuExceptionEnum;
import com.cloud.api.upms.model.SysMenu;
import com.cloud.common.satoken.util.LoginUtil;
import com.cloud.upms.dao.SysMenuMapper;
import com.cloud.upms.service.SysMenuService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Author:  xuanning
 * Date:    2023/2/9 15:27
 */
@Service
@AllArgsConstructor
public class SysMenuServiceImpl  extends ServiceImpl<SysMenuMapper, SysMenu> implements SysMenuService {

    private final SysMenuMapper sysMenuMapper;

    @Override
    public void addOrUpdate(MenuAddUpdateReq paramReq) {
        String menuName=paramReq.getMenuName();
        Long id=paramReq.getId();
        Long count=sysMenuMapper.selectCount(new LambdaQueryWrapper<SysMenu>()
                .eq(SysMenu::getMenuName,menuName)
                .eq(SysMenu::getPid,paramReq.getPid())
                .eq(SysMenu::getDelFlag, CommonConstants.ZERO)
                .ne(id!=null,SysMenu::getId,id)
        );
        if(count>0){
            throw new ProjectException(MenuExceptionEnum.MENUNAME_EXIST);
        }
        SysMenu menu=new SysMenu();
        menu.setId(id);
        menu.setMenuName(menuName);
        menu.setMenuCode(paramReq.getMenuCode());
        menu.setSort(paramReq.getSort());
        menu.setMenuType(paramReq.getMenuType());
        menu.setPid(paramReq.getPid());

        this.saveOrUpdate(menu);
    }

    @Override
    public void delete(Long id) {
        //有子菜单不能删除
        Long count=sysMenuMapper.selectCount(new LambdaQueryWrapper<SysMenu>()
                .eq(SysMenu::getPid,id)
                .eq(SysMenu::getDelFlag, CommonConstants.ZERO)
        );
        if(count>0){
            throw new ProjectException(MenuExceptionEnum.LEAF_EXIST);
        }
        sysMenuMapper.deleteById(id);
    }

    @Override
    public List<Tree<Long>> listMenu() {

        List<SysMenu> list=sysMenuMapper.selectList(new LambdaQueryWrapper<SysMenu>()
                .eq(SysMenu::getDelFlag, CommonConstants.ZERO)
        );
        return TreeUtil.build(list,0L,(node, tree)->{
            tree.setId(node.getId());
            tree.setName(node.getMenuName());
            tree.setParentId(node.getPid());
            //额外参数
            tree.putExtra("menuCode", node.getMenuCode());
            tree.putExtra("sort", node.getSort());
            tree.putExtra("menuType", node.getMenuType());
        });
    }

    @Override
    public List<Tree<Long>> listUserMenu() {
        if(LoginUtil.isAdmin()){
            return listMenu();
        }
        Long userId=LoginUtil.getUserId();
        List<SysMenu> list=sysMenuMapper.listUserMenu(userId);

        return TreeUtil.build(list,0L,(node, tree)->{
            tree.setId(node.getId());
            tree.setName(node.getMenuName());
            tree.setParentId(node.getPid());
            //额外参数
            tree.putExtra("menuCode", node.getMenuCode());
            tree.putExtra("sort", node.getSort());
            tree.putExtra("menuType", node.getMenuType());
        });
    }
}
